/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.start.controller;

import com.itgrail.pilot.app.command.cmo.*;
import com.itgrail.pilot.app.command.cmo.query.*;
import com.itgrail.pilot.infrastructure.tunnel.util.SessionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.itgrail.grail.cqrs.command.CommandBus;
import com.itgrail.grail.dto.ResultData;

/**
 * @author xujin
 */
@RestController
@RequestMapping("/admin/objective")
@Api("OKR目标管理")
public class ObjectiveController {

    @Autowired
    private CommandBus commandBus;

    @PostMapping("/add")
    @ApiOperation(value = "新增OKR目标")
    @RequiresPermissions("ork:objective:add")
    public ResultData add(@RequestBody AddObjectiveCmd cmd) {
        if (cmd.getCurrentUserId() == null) {
            cmd.setCurrentUserId(SessionUtils.currentUserId());
        }
        return commandBus.send(cmd);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新OKR目标")
    @RequiresPermissions("ork:objective:update")
    public ResultData update(@RequestBody UpdateObjectiveCmd cmd) {
        if (cmd.getCurrentUserId() == null) {
            cmd.setCurrentUserId(SessionUtils.currentUserId());
        }
        return commandBus.send(cmd);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除OKR目标")
    @RequiresPermissions("ork:objective:delete")
    public ResultData delete(@RequestBody DeleteObjectiveCmd cmd) {
        if (cmd.getCurrentUserId() == null) {
            cmd.setCurrentUserId(SessionUtils.currentUserId());
        }
        return commandBus.send(cmd);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "获取OKR详情")
    @RequiresPermissions("ork:objective:detail")
    public ResultData detail(@RequestBody DetailOKRCmd cmd) {
        if (cmd.getCurrentUserId() == null) {
            cmd.setCurrentUserId(SessionUtils.currentUserId());
        }
        return commandBus.send(cmd);
    }

    @PostMapping("/page")
    @ApiOperation(value = "获取OKR列表分页")
    @RequiresPermissions("ork:objective:page")
    public ResultData page(@RequestBody PageOKRCmd cmd) {
        if (cmd.getCurrentUserId() == null) {
            cmd.setCurrentUserId(SessionUtils.currentUserId());
        }
        return commandBus.send(cmd);
    }

    @PostMapping("/listUserOkrs")
    @ApiOperation(value = "查询某个用户的OKR---给查询搜索框用")
    @RequiresPermissions("ork:objective:listUserOkrs")
    public ResultData listUserOkrs(@RequestBody ListUserOKRCmd cmd) {
        if (cmd.getCurrentUserId() == null) {
            cmd.setCurrentUserId(SessionUtils.currentUserId());
        }
        return commandBus.send(cmd);
    }

    @PostMapping("/listUserOkrInfo")
    @ApiOperation(value = "查询指定用户的OKR---给对齐视图用")
    @RequiresPermissions("ork:objective:listUserOkrInfo")
    public ResultData listUserOkrInfo(@RequestBody ListMyOKRCmd cmd) {
        if (cmd.getCurrentUserId() == null) {
            cmd.setCurrentUserId(SessionUtils.currentUserId());
        }
        return commandBus.send(cmd);
    }

    @PostMapping("/listMySameLevelOkrs")
    @ApiOperation(value = "查询当前用户同级的OKR---给对齐视图用")
    @RequiresPermissions("ork:objective:listMySameLevelOkrs")
    public ResultData listMySameLevelOkrs(@RequestBody ListMySameLevelOKRCmd cmd) {
        if (cmd.getCurrentUserId() == null) {
            cmd.setCurrentUserId(SessionUtils.currentUserId());
        }
        return commandBus.send(cmd);
    }

    @PostMapping("/deptUsers")
    @ApiOperation("查询当前部门人员")
    @RequiresPermissions("ork:objective:listMySameLevelOkrs")
    public ResultData getDeptUserList(@RequestBody QueryDeptUserCmd queryDeptUserCmd) {
        return commandBus.send(queryDeptUserCmd);
    }

    @PostMapping("/getUserObjectiveAlign")
    @RequiresPermissions("ork:objective:getUserObjectiveAlign")
    @ApiOperation(value = "根据okrId查询当前OKR对齐的所有OKR---给查询对齐视图详情使用")
    public ResultData getUserObjectiveAlign(@RequestBody DetailUserOKRAlignCmd cmd) {
        if (cmd.getCurrentUserId() == null) {
            cmd.setCurrentUserId(SessionUtils.currentUserId());
        }
        return commandBus.send(cmd);
    }

    @PostMapping("/updateObjectSort")
    @ApiOperation(value = "更新O的排序")
    public ResultData updateObjectSort(@RequestBody UpdateObjectSortCmd cmd) {
        if (cmd.getCurrentUserId() == null) {
            cmd.setCurrentUserId(SessionUtils.currentUserId());
        }
        return commandBus.send(cmd);
    }


    @PostMapping("/updateKeySort")
    @ApiOperation(value = "更新Key的排序")
    public ResultData updateKeySort(@RequestBody UpdateKeySortCmd cmd) {
        if (cmd.getCurrentUserId() == null) {
            cmd.setCurrentUserId(SessionUtils.currentUserId());
        }
        return commandBus.send(cmd);
    }
}
